<template>
    <div class="assets">
        <p class="title">资产汇总</p>
        <ul>
            <li class="total_count">
                {{home.total_count}}
                <span>资产总数</span>
            </li>
            <li class="total_price">
                {{home.total_price}}
                <span>资产总额</span>
            </li>
            <li class="total_available">
                {{home.total_available}}
                <span>在用资产</span>
            </li>
            <li class="more" @click="go_summary">查看更多<i class="el-icon-d-arrow-right"></i></li>
        </ul>
    </div>
    <div class="unit" v-if="type">
        <div class="title">
            <p>单位管理</p>
            <span @click="go_unit">更多<i class="el-icon-d-arrow-right"></i></span>
        </div>
        <ul>
            <li v-for="item in unit_list" :key="item.company_id">
                <p class="unit_title">
                    <span>{{item.company_name}}</span>
                    <span @click="go_summary(item.company_id)">查看<i class="el-icon-d-arrow-right"></i></span>
                </p>
                <div class="unit_info">
                    <div>
                        <p>资产总数</p>
                        <p>{{item.total_count}}</p>
                    </div>
                    <div>
                        <p>资产总额</p>
                        <p>{{item.total_price}}</p>
                    </div>
                    <div>
                        <p>在用资产</p>
                        <p>{{item.total_available}}</p>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</template>

<script lang="js">
import { defineComponent, reactive, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import { home, unit_statistics } from './../utils/api'
export default defineComponent({
    setup() {
        const router = useRouter()
        let state = reactive({
            home: {},
            unit_list: [],
            type: 0
        })
        state.type = sessionStorage.user_info ?  Number(JSON.parse(sessionStorage.user_info).type) : 0

        // 资产汇总
        home().then(({ data }) => {
            if(data.code === 1000) {
                state.home = data.data
            }
        })
        // 单位管理
        if(state.type) {
            unit_statistics().then(({ data }) => {
                if(data.code === 1000) {
                    state.unit_list = data.data
                }
            })
        }

        // 跳转单位
        const go_unit = () => {
            router.push({path: '/unit'})
        }
        // 跳转资产
        const go_summary = company_id => {
            let company = undefined
            if(typeof company_id === 'number') {
                company = company_id
            }
            router.push({path: '/summary', query: {company_id: company}})
        }

        return {
            ...toRefs(state),
            go_unit,
            go_summary
        }
    }
})
</script>

<style lang="scss" scoped>
.assets{
    .title{
        font-weight: bold;
        font-size: 20px;
        margin-bottom: 10px;
    }
    ul{
        width: 100%;
        display: flex;
        li{
            margin-left: 1%;
            height: 200px;
            text-align: center;
            display: flex;
            flex-direction: column;
            justify-content: center;
            color: #fff;
            font-size: 18px;
            font-weight: bold;
            border-radius: 10px;
            width: 26%;
            span{
                margin-top: 16px;
                font-size: 16px;
            }
            &.total_count{
                box-shadow: 0px 5px 9px 1px rgba(78,81,88,0.34); 
                background-color: #8794EC;
            }
            &.total_price{
                background-color: #B590E8;
                box-shadow: 0px 5px 9px 1px rgba(78,81,88,0.34); 
            }
            &.total_available{
                background-color: #95C1F3;
                box-shadow: 0px 5px 9px 1px rgba(78,81,88,0.34); 
            }
            &.more{
                cursor: pointer;
                flex-direction: row;
                line-height: 200px;
                width: 19%;
                background-color: #F3AC9B;
                box-shadow: 0px 5px 9px 1px rgba(78,81,88,0.34);
                i{
                    align-self: center;
                }
            }
            &:first-child{
                margin-left: 0;
            }
        }
    }
}
.unit{
    margin-top: 25px;
    .title{
        margin-bottom: 10px;
        display: flex;
        justify-content: space-between;
        >p{
            font-weight: bold;
            font-size: 20px;
            margin-bottom: 10px;
        }
        span{
            cursor: pointer;
            background-color: #4E8BF7;
            padding: 8px 20px;
            color: #fff;
            border-radius: 20px;
        }
    }
    ul{
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        li{
            background-color: #fff;
            width: 49.5%;
            box-shadow: 0px 2px 5px 0px rgba(78,81,88,0.19);
            border-radius: 10px;
            margin-bottom: 20px;
            &:nth-child(2n) {
                margin-right: 0;
            }
            .unit_title{
                display: flex;
                justify-content: space-between;
                padding: 10px;
                border-bottom: 1px solid #4E5158;
                span{
                    &:first-child{
                        font-size: 16px;
                        font-weight: bold;
                    }
                    &:last-child{
                        color: #999;
                        cursor: pointer;
                    }
                }
            }
            .unit_info{
                padding: 30px 10px;
                display: flex;
                >div{
                    width: calc(100% / 3);
                    text-align: center;
                    p{
                        font-weight: bold;
                        &:first-child{
                            margin-bottom: 10px;
                        }
                    }
                }
            }
        }
    }
}
</style>